<?php

class PNCarrentSearchArray extends PNObjectArray {
  function PNCarrentSearchArray($init=null, $where='')
  {
    $this->PNObject();  

    $this->_objType       = 'icarrent_topics';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_category',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');
                            

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_subcategory',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('subcategory_name'),
                            'compare_field_table' =>  'subcategory_id',
                            'compare_field_join'  =>  'id');
                            

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_brand',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('brand_name'),
                            'compare_field_table' =>  'brand_id',
                            'compare_field_join'  =>  'id');   
                            

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_model',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('model_name'),
                            'compare_field_table' =>  'model_id',
                            'compare_field_join'  =>  'id');       
                            
                            

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_shop',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('shop_name'),
                            'compare_field_table' =>  'shop_id',
                            'compare_field_join'  =>  'id');                                                                                                          
  }



  function genFilter(){
  
    $form  = FormUtil::getPassedValue ('form', false, 'REQUEST');  
    $location1  = FormUtil::getPassedValue ('location1', false, 'REQUEST');      
  
    /////////////////////////////  
    //Search by location 
    /////////////////////////////
    //Select province 
    $tablename = "icarrent_province";
    $where = " prv_name = '".$location1."'";
    $provinceArray = DBUtil::selectObject( $tablename, $where, null, null, null, true);
  
    //print_r($provinceArray);  exit;
    //Select shop     
    if($location1 && $provinceArray['id']){
    
      $tablename = "icarrent_pickup_location";
      $where = " pik_province_id = ".$provinceArray['id'];
      $shopArray = DBUtil::selectObject( $tablename, $where, null, null, null, true);
      //print_r($shopArray); exit;
      //$wheres[] = " tpc_pickup_location = '". strtolower($form['pickUpLocation']) ."'"; 
      //$wheres[] = " tpc_pickup_location = '". strtolower($form['pickUpLocation']) ."'";        
    }
    //Select Cars         
    if($location1 && $provinceArray['id'] && $shopArray['shop_id']){
    
      $tablename = "icarrent_topics";
      $wheres[] = " tpc_shop_id = ".$shopArray['shop_id'];  
    }    
    
    
    /////////////////////////////  
    //Search by price range 
    /////////////////////////////
    $price_range  = FormUtil::getPassedValue ('price_range', false, 'REQUEST');      
    print_r($price_range); exit;
        
    
/*
    $wheres = array();
  
    if($form['pickUpLocation']){
       $wheres[] = " tpc_pickup_location = '". strtolower($form['pickUpLocation']) ."'"; 
    }
*/    
    

 /*    
    if($form['dropoffLocation']){
       $wheres[] = " tpc_dropoff_location = '". strtolower($form['dropoffLocation']) ."'"; 
    }    
   
    if($form['startDate']){
       $wheres[] = " tpc_dropoff_location = '". $form['startDate'] ."'"; 
    }    


    if($form['endDate']){
       $wheres[] = " tpc_dropoff_location = '". $form['endDate'] ."'"; 
    }    


    if($form['carSize']){
       $wheres[] = " tpc_type = '". $form['carSize'] ."'"; 
    }    

    if($form['priceRange']){
    
    
       $priceRangeSplit = explode(' ', $form['priceRange']);
       
       $priceStartSplit = explode('$', $priceRangeSplit[0]);
       $priceEndSplit = explode('$', $priceRangeSplit[2]);

       $wheres[] = " tpc_price_day BETWEEN ".$priceStartSplit[1]." AND ".$priceEndSplit[1]; 
    }       
    */
    
    //print_r($wheres); exit;
    
    //print_r($form['pickUpLocation']);
/*    
    $form[dropoffLocation] => phuket
    $form[startDate] => 14/04/2012 20:51
    $form[endDate] => 24/09/2012 20:51
    $form[carSize] => All Size
    $form[priceRange] => $0 - $500
    
*/    

//$whereTest = implode(' AND ', $wheres);
//print_r($whereTest);  exit;  

    return implode(' AND ', $wheres) ;
  }
  

}